SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE Procedure [dbo].[SP_TMDOCU_PROV_Q09]  
/*-------------------------------------------------------*/  
/*---Empresa              : OFISIS S.A.                --*/  
/*---Cliente              : CPPQ --*/  
/*---Sistema              : Tesoreria                  --*/  
/*---M®dulo               : Empresa                    --*/  
/*---Programa             : Carga de Tabla --*/  
/*---                       para las guias de ingreso de logistica --*/  
/*---Script               :      --*/  
/*---Nombre SP            : SP_TCMOVI_BANC_I01         --*/  
/*---Desarrollado por     : Javier Flores A.           --*/  
/*---Fecha Creaci®n       : 11/06/1999                 --*/  
/*---Base Datos           : Microsoft Sql Server       --*/  
/*---Versi®n              : 8.0                        --*/  
/*---Invoca a SP          :                            --*/  
/*-------------------------------------------------------*/  
  
/*-------------------------------------------------------*/  
/*---Modificado 1 por     :    DCHIANG                 --*/  
/*---Fecha Modificaci®n   :    18/03/2009               --*/   
/*---Detalle Modificaci®n :Agregar CO_ALMA y CO_UNID   --*/  
/*-------------------------------------------------------*/  
  
/*-------------------------------------------------------*/  
/*---Comentarios                                       --*/  
/*---                                                  --*/  
/*---                                                  --*/  
/*-------------------------------------------------------*/  
  
/*-------------------------------------------------------*/  
/*---Drop Proc SP_TMDOCU_PROV_Q09                      --*/  
/*---Grant all on SP_TMDOCU_PROV_Q09 to public         --*/   
/*---SP_TMDOCU_PROV_Q09                                --*/  
/*-------------------------------------------------------*/  
 @ISCO_EMPR TD_VC_002,   
 @ISCO_PROV TD_VC_020,  
 @ISCO_TIPO_DOCU   TD_VC_003,  
 @ISNU_DOCU_PROV TD_VC_020  
 as  

Declare @VSNU_DOCU_ORIG  Varchar(20)
  
Create table #TWDOCU_PROV_Q09(TI_DOCU_ORIG Varchar(3) Null,    
     NU_DOCU_ORIG Varchar(20) Null,   
     CO_MONE Varchar(3) Null,   
     TI_DOCU_GUIN Varchar(3) Null,   
     NU_DOCU_GUIN Varchar(20) Null,   
     IM_DOCU_ORIG numeric(16,4) Null,  
     IM_CONV numeric(16,4) Null,  
     NU_CORR int Null,  
     ST_FLAG Varchar(1) Null,   
     CO_UNID Varchar(3) Null,   
     CO_ALMA Varchar(3) Null)   

Select @VSNU_DOCU_ORIG  = ''

Select  @VSNU_DOCU_ORIG =  T1.NU_DOCU_ORIG
From  TDORDE_FACT T1  
 Where T1.CO_EMPR = @ISCO_EMPR   
  And T1.CO_PROV =  @ISCO_PROV  
  And T1.CO_TIPO_DOCU = @ISCO_TIPO_DOCU   
  And T1.NU_DOCU_PROV = @ISNU_DOCU_PROV  

  
Insert Into #TWDOCU_PROV_Q09(TI_DOCU_ORIG, NU_DOCU_ORIG, CO_MONE,   
         TI_DOCU_GUIN, NU_DOCU_GUIN, IM_DOCU_ORIG,   
         IM_CONV , NU_CORR, ST_FLAG, CO_UNID, CO_ALMA)  
Select  T1.TI_DOCU_ORIG,  T1.NU_DOCU_ORIG, T1.CO_MONE,  
  T1.TI_DOCU_GUIN, T1.NU_DOCU_GUIN, T1.IM_DOCU_ORIG,  
  T1.IM_CONV, T1.NU_CORR, 'S', T1.CO_UNID, T1.CO_ALMA
 From  TDORDE_FACT T1  
 Where T1.CO_EMPR = @ISCO_EMPR   
  And T1.CO_PROV =  @ISCO_PROV  
  And T1.CO_TIPO_DOCU = @ISCO_TIPO_DOCU   
  And T1.NU_DOCU_PROV = @ISNU_DOCU_PROV  

If len(@VSNU_DOCU_ORIG)> 0 
		Update #TWDOCU_PROV_Q09  
		Set CO_UNID = t3.CO_UNID,   
		    CO_ALMA = t3.CO_ALMA  
		From #TWDOCU_PROV_Q09 t1, TCORDE_COMP t2, TCDOCU_ALMA t3  
		Where t3.CO_EMPR = @ISCO_EMPR   
		And t3.TI_DOCU = t1.TI_DOCU_GUIN  
		And t3.NU_DOCU = t1.NU_DOCU_GUIN  
		And t3.CO_EMPR = t2.CO_EMPR  
		And t3.NU_ORCO = t2.NU_ORCO  

  
Select TI_DOCU_ORIG, NU_DOCU_ORIG, CO_MONE, TI_DOCU_GUIN, NU_DOCU_GUIN, IM_DOCU_ORIG,   
       IM_CONV , NU_CORR, ST_FLAG, CO_UNID, CO_ALMA  
From #TWDOCU_PROV_Q09   
Order by TI_DOCU_ORIG, NU_DOCU_ORIG, TI_DOCU_GUIN, NU_DOCU_GUIN  
/*------------------------ Fin ------------------------------*/


GO
